<template>
  <div class="shopping-cart" id="app">
    <div class="header-nav">
      <ul>
        <li>全部(20)</li>
        <li>降价(4)</li>
        <li>常卖(2)</li>
        <li>分类</li>
      </ul>
    </div>
    <!-- 主体 -->
    <div class="content">
      <div class="content-top">
        <div class="content-topl">
          <input type="checkbox" v-model="checkedAll" @change="checkedall"/>
          <span>小U自营</span>
        </div>
        <div class="content-topr">
          <span>已免运费</span>
          <span></span>
          <span>领券</span>
        </div>
      </div>
      <div class="content-bottom">

        <div class="content1" v-if="list.length==0">
          <i class="iconfont icon-aixin"></i>
          <p>购物车暂时没有商品~~</p>
          <button round type="warning" size="normal" @click="$router.push('/index')">随便逛逛</button>
        </div>

        <div class="content-bottomBox" v-for="(item) in list" :key="item.id">
          <input type="checkbox" v-model="item.checked" @change="one" />
          <img :src="$pre+item.img" />
          <div class="text">
            <p>{{item.goodsname}}</p>
            <!-- <p>规格：{{item.goodsname1}}</p> -->
            <div class="text-xia">
              <div class="text-xiaz">
                <span>比加入时降低&yen;100</span>
                <span>
                  <em>
                    <i>&yen;</i>
                    {{item.num*item.price | filterprice}}
                  </em>
                  <s>&yen;{{item.price }}</s>
                </span>
              </div>
              <div class="text-xiay">
                <span @click="sub(item.id,item.num)"><van-icon name="minus" /></span>
                <!-- <span>[{{item.num}}]</span> -->
                <van-button plain type="info">{{item.num}}</van-button>
                <span @click="add(item.id)"><van-icon name="plus" /></span>
                <button class="del" @click="del(item.id)">删除</button>
                <!-- <van-icon name="delete" color="red" class="del"/> -->
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="fff">
      <div class="footer">
        <form action="#" class="fl">
          <input type="checkbox" v-model="checkedAll" @change="checkedall" />
          <h2>全选</h2>
        </form>
        <div class="fl">
          <div class="top">
            <p>
              合计
              <span>&yen;{{he | filterprice}}</span>
            </p>
          </div>
          <div class="x">
            <p>
              已免运费
              <span>以优惠&yen;200元</span>
            </p>
          </div>
        </div>
        <div class="fr" @click="toqrdd">
          <div class="q">
            <a href="javascript:;">去结算({{jian}}件)</a>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { Toast } from "vant";
import { reqcartlist, reqcartedit, reqcartdelete } from "../../http/api";
export default {
  data() {
    return {
      checkedAll: false,
      list: [],
      isshow: true,
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      let checkarr = [...this.list];
      reqcartlist({
        uid: JSON.parse(localStorage.getItem("user")).uid,
      }).then((res) => {
        this.isshow = true;
        let list = res.data.list ? res.data.list : [];
        list.forEach((item) => {
          item.checked = checkarr.find((i) => i.id === item.id)
            ? checkarr.find((i) => i.id === item.id).checked
            : false;
        });
        this.list = list;
        // console.log(res);
      });
    },
    //数量加
    add(id) {
      reqcartedit({
        id: id,
        type: 2,
      }).then((res) => {
        if (res.data.code == 200) {
          Toast(res.data.msg);
          this.init();
        }
      });
    },
    //数量减
    sub(id, num) {
      if (num <= 1) {
        Toast("最后一个了！！！");
        return;
      }
      if (this.isshow) {
        this.isshow = false;

        reqcartedit({
          id: id,
          type: 1,
        }).then((res) => {
          if (res.data.code == 200) {
            Toast(res.data.msg);
            this.init();
          }
        });
      }
    },
    //全选
    checkedall() {
      this.list.forEach((item) => {
        item.checked = this.checkedAll;
      });
    },
    //一个不选 全选框就false
    one() {
      this.checkedAll = this.list.every((item) => item.checked);
    },
    del(id) {
      reqcartdelete({ id: id }).then((res) => {
        Toast(res.data.msg);
        this.init();
      });
    },
    toqrdd() {
      if (this.jian == 0) {
        Toast("还没选择商品");
        return;
      } else {
        let str = this.list.reduce(
          (val, item) => (item.checked ? (val += item.id + ",") : val),
          ""
        );
        let json = {
          countmoney: this.he,
          countnumber: this.list.reduce(
            (val, item) => (item.checked ? (val += item.num) : val),
            0
          ),
          idstr: str.slice(0, str.length - 1),
          price:this.he
        };
        localStorage.setItem("order", JSON.stringify(json));
        this.$router.push("/qrdd");
      }
    },
  },
  computed: {
    he() {
      return this.list.reduce(
        (val, item) => (item.checked ? (val += item.price * item.num) : val),
        0
      );
    },
    jian() {
      return this.list.reduce(
        (val, item) => (item.checked ? (val += item.num) : val),
        0
      );
    },
  },
};
</script>

<style scoped>
.van-icon-minus::before,.van-icon-plus::before{
  margin-top: 0.2rem;
}
.van-button--plain.van-button--info{
  width: .6rem;
  height: .6rem;
}
.content1 {
  width: 100%;
  height: 100%;
  text-align: center;
  position: absolute;
  top:2.4rem;
}
.content1 i {
  font-size: 2rem;
  color: #ff6040;
}
.content1 p {
  font-size: 0.4rem;
  margin: 0.2rem 0;
  color: #ff6040;
}
.content1 button {
  width: 3rem;
  height: 0.7rem;
  background-color: #ff6040;
  border-radius: 0.3rem;
  color: #fff;
  border: none;
}
.shopping-cart {
  display: flex;
  flex-direction: column;
  width: 100%;
  min-width: 3.2rem;
  max-width: 7.5rem;
}
/* to */
.header-nav {
  position: relative;
  width: 100vw;
  height: 0.32rem;
  background-image: linear-gradient(180deg, #ff6040 0%, #ff8a80 100%);
  box-shadow: 0 0.02rem 0.06rem 0 rgba(255, 149, 128, 0.5);
  padding-bottom: 0.2rem;
}
.header-nav ul {
  display: flex;
  margin-top: 0.1rem;
}
.header-nav ul li {
  width: 25%;
  height: 100%;
  font-size: 0.34rem;
  color: #fff3f2;
  text-align: center;
  line-height: 0.32rem;
}
.header-nav span {
  position: absolute;
  bottom: 0rem;
  left: 0.25rem;
  width: 0.2rem;
  height: 0.02rem;
  background-color: #ffffff;
}

/* 主体 */
.content {
  display: flex;
  width: 100%;
  height: 100%;
  flex: 1;
  flex-direction: column;
}
.content-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  height: 1rem;
  background-color: #ffffff;
}
.content-topl {
  padding-left: 20px;
}
.content-topl span {
  font-size: 0.3rem;
  font-weight: 600;
  color: #333333;
}
/* input样式 */
.content-topr {
  padding-right: 20px;
}
.content-topr span {
  font-size: 0.3rem;
  color: #333333;
}
.content-topr span:nth-of-type(2) {
  margin: 0px 2px;
  display: inline-block;
  width: 1px;
  height: 10px;
  background-color: #000;
}
.content-topr span:nth-of-type(3) {
  color: #ff6040;
}
.content-bottom {
  padding-bottom: 4rem;
  display: flex;
  flex-direction: column;
  justify-content: space-evenly;
  width: 100vw;
  background-color: #ffffff;
  position: relative;
}
.content-bottomBox {
  display: flex;
  width: 100%;
  height: 2rem;
  align-items: center;
  padding: 0.5rem 0;
}
.content-bottomBox input {
  margin-left: 0.1rem;
}
.content-bottomBox img {
  margin: 0px 10px;
  display: block;
  width: 1.08rem;
  /* height: 100%; */
}
.text {
  /* width: 216px; */
  height: 100%;
}
.text p:nth-of-type(1) {
  font-size: 0.3rem;
  color: #333333;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin: 0.2rem 0;
}
.text p:nth-of-type(2) {
  font-size: 0.2rem;
  color: #999999;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.text-xia {
  margin-top: 0.1rem;
  display: flex;
  position: relative;
  width: 100%;
  height: auto;
}
.text-xiaz {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
}
.text-xiaz span:nth-of-type(1) {
  font-size: 0.3rem;
  line-height: 0.16rem;
  color: red;
}
.text-xiaz span:nth-of-type(2) {
  font-size: 0.3rem;
  color: #ff6040;
  margin-top: 0.3rem;
}
.text-xiaz span s {
  color: #cccccc;
}
.text-xiay {
  position: absolute;
  bottom: 0.25rem;
  right: -2.9rem;
}
.text-xiay .del {
  width: 50px;
  height: 30px;
  font-size: 0.16rem;
  border: none;
  background-color: #ed4014;
  color: #fff;
}
.text-xiay span {
  font-size: 0.48rem;
  vertical-align: middle;
  margin: 0  0.1rem;

}
.fff {
  position: relative;
}
.fff .footer {
  position: fixed;
  left: 0;
  bottom: 1.17rem;
  width: 100%;
  padding-left: 0.2rem;
  padding-right: 0.2rem;
  background: white;
  border-top: 1px solid #cccccc;
}
.fff .footer form {
  display: flex;
  align-items: center;
  margin-right: 0.7rem;
}
.fff .footer form input {
  width: 0.29rem;
  height: 0.29rem;
  border: 0.02rem solid #9f9f9f;
  background: #f2f2f2;
  border-radius: 50%;
}
.fff .footer form h2 {
  font: bold 0.31rem/0.98rem "微软雅黑";
  margin-left: 0.2rem;
}
.fff .footer .fl {
  margin-top: 0.1rem;
}
.fff .footer .fl .top p {
  font: bold 0.31rem "微软雅黑";
  color: black;
}
.fff .footer .fl .top p span {
  color: #ff6040;
  margin-left: 0.64rem;
}
.fff .footer .fl .x {
  margin-top: 0.11rem;
}
.fff .footer .fl .x p {
  font: 0.19rem "微软雅黑";
  color: #cfcfcf;
}
.fff .footer .fl .x p span {
  margin-left: 0.36rem;
}
.fff .footer .fr {
  width: 2.2rem;
  height: 1.08rem;
  background: #ff6040;
}
.fff .footer .fr .q {
  text-align: center;
}
.fff .footer .fr .q a {
  font: bold 0.26rem/1.08rem "微软雅黑";
  color: white;
}
</style>